/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpdisenio2009.model.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import tpdisenio2009.model.HibernateUtils;
import tpdisenio2009.model.beans.FantasyTeam;
import tpdisenio2009.model.beans.FantasyTeamScoreByTournamentDate;
import tpdisenio2009.model.beans.TournamentDate;
import tpdisenio2009.model.beans.FantasyTeamPlayer;
import tpdisenio2009.model.beans.User;

/**
 *
 * @author Pablito
 */
public class FantasyTeamDAO extends AbstractDAO<FantasyTeam> {

    @Override
    public Class<FantasyTeam> getPersistentClass() {
        return FantasyTeam.class;
    }

    /*
    public List<FantasyTeam> getFantasyTeamsByTournamentDate( TournamentDate date, int page ) {
        List<FantasyTeam> teams = new ArrayList<FantasyTeam>();
        List<FantasyTeamScoreByTournamentDate> consulta;

        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamScoreByTournamentDate.class );
        mainCriteria.add( Restrictions.eq( "tournamentDate", date ) );
        mainCriteria.addOrder( Order.desc( "score" ) );

        mainCriteria.setFirstResult( page * Constants.PAGE_SIZE );
        mainCriteria.setMaxResults( Constants.PAGE_SIZE );

        consulta = mainCriteria.list();

        for ( FantasyTeamScoreByTournamentDate team : consulta ) {
            teams.add(team.getFantasyTeam());
        }

        return teams;
    }*/

    public List<FantasyTeam> getFantasyTeamsByTournamentDate( TournamentDate date ) {
        List<FantasyTeam> teams = new ArrayList<FantasyTeam>();
        List<FantasyTeamScoreByTournamentDate> consulta;

        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamScoreByTournamentDate.class );
        mainCriteria.add( Restrictions.eq( "tournamentDate", date ) );

        consulta = mainCriteria.list();

        for ( FantasyTeamScoreByTournamentDate team : consulta ) {
            teams.add(team.getFantasyTeam());
        }

        return teams;
    }

    public Integer getLlevasGastado( FantasyTeam fantasyTeam ) {
        Session session = HibernateUtils.getCurrentSession();
        
        Criteria mainCriteria = session.createCriteria( FantasyTeamPlayer.class );
        mainCriteria.add( Restrictions.eq( "fantasyTeam", fantasyTeam ) );
        mainCriteria.add( Restrictions.eq( "endDate", null) );
        List<FantasyTeamPlayer> players = mainCriteria.list();

        Integer gastado = 0;
        for (FantasyTeamPlayer player : players) {
            gastado += player.getSoccerPlayer().getPrice();
        }
        return gastado;
    }

    public FantasyTeamScoreByTournamentDate getFantasyTeamPointsByDate( FantasyTeam fantasyTeam , TournamentDate date ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamScoreByTournamentDate.class );
        mainCriteria.add( Restrictions.eq( "tournamentDate", date ) );
        mainCriteria.add( Restrictions.eq( "fantasyTeam", fantasyTeam ) );

        return (FantasyTeamScoreByTournamentDate) mainCriteria.uniqueResult();
    }
    
    public List<FantasyTeamPlayer> getCurrentPlayers( User user ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamPlayer.class ).createAlias( "fantasyTeam", "team" );
        mainCriteria.add( Restrictions.eq( "team.user", user ) );        
        mainCriteria.add( Restrictions.isNull( "endDate" ) );

        return mainCriteria.list();
    }

    public int getFantasyTeamGlobalPosition( FantasyTeam fantasyTeam ) {
        List<FantasyTeam> equipos;
        int pos = 1;

        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeam.class );
        mainCriteria.addOrder( Order.desc( "totalScore" ) );

        equipos = mainCriteria.list();

        for (FantasyTeam equipo : equipos) {
            if ( equipo.equals( fantasyTeam ) ) {
                return pos;
            }
            pos++;
        }
        return 0;
    }

    public int getChangesCountInDate( User user, TournamentDate date ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamPlayer.class ).createAlias( "fantasyTeam", "team" );
        mainCriteria.add( Restrictions.eq( "team.user", user ) );
        mainCriteria.add( Restrictions.ge( "endDate", date.getInitialDate() ) );
        mainCriteria.add( Restrictions.le( "endDate", date.getBanDate() ) );
        mainCriteria.setProjection( Projections.count( "id" ) );

        return ((Integer) mainCriteria.uniqueResult()).intValue();
    }

    public List<FantasyTeamPlayer> getChangesInDate( User user, TournamentDate date ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamPlayer.class );
        mainCriteria.add( Restrictions.eq( "fantasyTeam", user.getFantasyTeam() ) );
        
        Criterion endDateCheck = Restrictions.and( Restrictions.ge( "endDate", date.getInitialDate() ) , Restrictions.le( "endDate", date.getBanDate() ) );
        Criterion beginDateCheck = Restrictions.and( Restrictions.ge( "beginDate", date.getInitialDate() ) , Restrictions.le( "beginDate", date.getBanDate() ) );
        
        mainCriteria.add( Restrictions.or( endDateCheck, beginDateCheck ) );

        return mainCriteria.list();
    }

    public List<FantasyTeam> getFantasyTeamsByName( String name ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeam.class );
        mainCriteria.add( Restrictions.eq( "name", name ) );

        return mainCriteria.list();
    }

}
